package org.jfk.tbox.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jfk.core.controller.BaseController;
import org.jfk.core.utils.ByteUtils;
import org.jfk.sys.user.User;
import org.jfk.tbox.model.TboxDevice;
import org.jfk.tbox.model.TboxMsgLog;
import org.jfk.tbox.net.TboxMsg;
import org.jfk.tbox.net.TboxMsgParser;
import org.jfk.tbox.net.app.DSRC_AbnormalAlarm.DSRC_AbnormalAlarmReq;
import org.jfk.tbox.net.app.TBOX_AbnormalAlarm.TBOX_AbnormalAlarmReq;
import org.jfk.tbox.net.app.TBOX_BCall.TBOX_BCallReq;

public class TboxMsgLogController extends BaseController {
	private static Logger logger = Logger.getLogger(TboxMsgLogController.class);  
	
	public void edit() {
		TboxMsgLog tboxMsgLog = TboxMsgLog.dao.findById(getParaToInt());
		if (tboxMsgLog == null){
			tboxMsgLog = new TboxMsgLog();
		}
		setAttr("tboxMsgLog", tboxMsgLog);
	}
	
	public void dispose() {
		TboxMsgLog tboxMsgLog = TboxMsgLog.dao.findById(getParaToInt());
		if (tboxMsgLog == null){
			tboxMsgLog = new TboxMsgLog();
		}
		
		byte[] a = ByteUtils.hexStringToBytes(tboxMsgLog.getStr("data"));
		
		TboxMsg m = TboxMsgParser.loadMsg(a);
		
		TboxDevice tboxDevice = TboxDevice.dao.findBySN(tboxMsgLog.getStr("sn"));
		setAttr("tboxMsgLog", tboxMsgLog);
		setAttr("tboxDevice", tboxDevice);

		if (m instanceof DSRC_AbnormalAlarmReq){
			DSRC_AbnormalAlarmReq msg = (DSRC_AbnormalAlarmReq)m;
			String eventDesc = msg.getEventDesc();
			int[] info = msg.getInfo();
			for (int i = 0 ; i < info.length ; i++){
				if (info[i] == 1) {
					setAttr("style"+(i+1),"style=\"border-color: #f00;z-index: 2;\"");
				}
			}
			setAttr("eventDesc",eventDesc);
			logger.debug("DSRC_AbnormalAlarmReq = " + msg);
		}else if (m instanceof TBOX_AbnormalAlarmReq){
			TBOX_AbnormalAlarmReq msg = (TBOX_AbnormalAlarmReq)m;
			String eventDesc = msg.getEventDesc();
			setAttr("eventDesc",eventDesc);
		}else if (m instanceof TBOX_BCallReq){
			TBOX_BCallReq msg = (TBOX_BCallReq)m;
			String eventDesc = msg.getEventDesc();
			setAttr("eventDesc",eventDesc);
		}
		
		setAttr("lng",121.5477440000);
		setAttr("lat",31.2524230000);
	}	

	public void save() throws Exception {
		
		TboxMsgLog tboxMsgLog = getModel(TboxMsgLog.class);
		if (tboxMsgLog.get("id") == null){
			User currUser = super.getCurrUser();
			tboxMsgLog.set("creator", currUser.getInt("id"));			
			tboxMsgLog.save();
		}else{
			//对于可以人工输入ID的场景需要增加此判断
			Object o = TboxMsgLog.dao.findById(tboxMsgLog.get("id"));
			if (o == null){
				tboxMsgLog.save();
			}else{
				tboxMsgLog.update();
			}
		}
		super.renderJson();
	}	
	
	public void disposeSave() throws Exception{
		TboxMsgLog tboxMsgLog = getModel(TboxMsgLog.class);
		if (tboxMsgLog.get("id") == null){
			
		}else{
			TboxMsgLog old = TboxMsgLog.dao.findById(tboxMsgLog.get("id"));
			if (old == null){
				
			}else{
				old.set("isCallUser", null);
				old.set("isECall", null);
				old.set("isRescue", null);
				//old.setAttrs(tboxMsgLog);
				old.put(tboxMsgLog);
				old.update();
			}
		}
		super.renderJson();
	}
	
	public void list(){
		listColumns = new String[][] { 
				{ "id", "编号" }, 
				{ "aid", "业务ID" },
				{ "sn", "设备标识号" }, 
				{ "imsi", "IMSI号码" }, 
				{ "vin", "车辆VIN码" },
				{ "eventTime", "发生时间" },
				{ "ip", "终端网络地址" },
				{ "disposeStatusName", "处理状态" },
				{ "data", "报文" }
				};

		super.setAttrs();
		
		setAttr("page",TboxMsgLog.dao.paginate(this));
		
	}
	
	public void list2dispose(){
		listColumns = new String[][] { 
				{ "id", "编号" }, 
				{ "aid", "业务ID" ,"0" },
				{ "vPlate", "车牌号码" },
				{ "eventId", "事件ID" ,"0" },
				{ "sn", "设备标识号" ,"0"}, 
				{ "vUser", "车主" }, 
				{ "vin", "车辆VIN码" ,"0"},
				{ "eventTime", "发生时间" },
				{ "alertType_cnName", "告警类型" },
				{ "disposeStatusName", "处理状态" }
				};

		super.setAttrs();
		
		super.setAttr("filter", "{\"tbox_msglog__disposeStatus\":{\"op\":\"eq\",\"value\":\"101000\"}}");
		
		super.setAttr("order","desc");
		
		super.setAttr("sort","id");
		
		setAttr("page",TboxMsgLog.dao.paginate(this));
		
	}
	
	public void del() {
		String id = getPara("id");
		TboxMsgLog.dao.deleteById(id);
		super.renderText("test del");
	}

	
	public void delAll() {
		Integer[] checkedId = super.getParaValuesToInt("id[]");

		for (int i = 0; checkedId != null && i < checkedId.length; i++) {
			TboxMsgLog.dao.deleteById(checkedId[i]);
		}
		super.renderText("test delAll");
	}
	
	public void getUndisposeMsgCount(){
		int undisposeMsgCount = TboxMsgLog.dao.getUndisposeMsgCount();
		Map m = new HashMap();
		m.put("undisposeMsgCount", undisposeMsgCount);
		super.renderJson(m);
	}
	

}
